package myapp.framelibrary.skin;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.TextUtils;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import myapp.framelibrary.base.BaseSkinActivity;
import myapp.framelibrary.skin.attrs.SkinView;
import myapp.framelibrary.skin.callback.ISkinChangeListener;
import myapp.framelibrary.skin.config.SkinPreUtils;

/**
 * Created by 超 on 2017/7/4.
 */

public class SkinManager {

    private static SkinManager instance;
    private Context mContext;
    private static Map<ISkinChangeListener,List<SkinView>> mSkinView = new HashMap<>();

    private SkinResource mSkinResource;
    public static SkinManager getInstance() {
        return instance;
    }

    public int loadSkin(String skinPath) {
        mSkinResource = new SkinResource(mContext,skinPath);

        File file = new File(skinPath);

        if (!file.exists()){
            return -2;
        }

        String currentSkinPath = SkinPreUtils.getInstance().getSkinPath();
        if (skinPath.equals(currentSkinPath)){
            return -1;
        }

        //保存当前皮肤状态
        saveShin(skinPath);
        return 0;

    }

    private void  _changeSkin() {
        for (Map.Entry<ISkinChangeListener, List<SkinView>> activityListEntry : mSkinView.entrySet()) {
            for (SkinView skinView : activityListEntry.getValue()) {
                skinView.skin();
            }
            activityListEntry.getKey().changeSkin(mSkinResource);
        }

    }

    private void saveShin(String skinPath) {
        SkinPreUtils.getInstance().saveSkinPath(skinPath);
    }

    public SkinResource getSkinResource() {
        return mSkinResource;
    }

    public void init(Context context){
       this. mContext =context.getApplicationContext();
        String skinPath = SkinPreUtils.getInstance().getSkinPath();
        File file = new File(skinPath);

        if (!file.exists()){
            SkinPreUtils.getInstance().clearSkinInfo();
        }

       String packageName = context.getPackageManager()
                .getPackageArchiveInfo(skinPath, PackageManager.GET_ACTIVITIES).packageName;

        if (!TextUtils.isEmpty(packageName)){
            SkinPreUtils.getInstance().clearSkinInfo();
        }
        mSkinResource = new SkinResource(skinPath);
    }

    public int restoreDefault() {

        String skinPath = SkinPreUtils.getInstance().getSkinPath();
        if (TextUtils.isEmpty(skinPath)){
            return -1;
        }

         skinPath = mContext.getPackageResourcePath();

        mSkinResource = new SkinResource(mContext,skinPath);

        _changeSkin();

        return 1;

    }

    public List<SkinView> getSkinViews(Activity a) {
        return mSkinView.get(a);
    }


    public void register(ISkinChangeListener listener, List<SkinView> skinViews) {
        mSkinView.put(listener,skinViews);
    }

    public void checkChangeSkin(SkinView skinView) {
        String currentSkinPath = SkinPreUtils.getInstance().getSkinPath();

        if (!TextUtils.isEmpty(currentSkinPath)){
            skinView.skin();
        }
    }

    static class Hodler {
        public static SkinManager getInstance() {
            return new SkinManager();
        }
    }
}
